package com.leetcode.No0921;

import org.junit.Test;

/**
 * @program: leetcode
 * @description: 使括号有效的最少添加
 * @author: wangzhihua
 * @date: 2022-10-12
 */
public class Solution {
	public int minAddToMakeValid(String s) {
		int len = s.length();
		int count = 0; // ( +1, ) -1
		int ans = 0;
		for (int i = 0; i < len; ++i) {
			count += (s.charAt(i) == '(') ? 1 : -1;
			if (count < 0) {
				++ans;
				count = 0;
			}
		}
		ans += count; // 若count!=0，则只能>0
		return ans;
	}

	@Test
	public void test1() {
		String s = "())";
		System.out.println(this.minAddToMakeValid(s));

		s = "(((";
		System.out.println(this.minAddToMakeValid(s));
	}
}
